{extend name="../../admin/view/main"}

{block name="content"}

<div class="think-box-shadow padding-0">
    <form onsubmit="return false;" id="DataForm" class='layui-form layui-card' autocomplete="off">
        <div class="layui-card-header text-center margin-20 font-w7 color-text layui-bg-gray border-radius-5">
            {$title|default='图片数据管理'}<span class="color-desc font-s12"> ( 建议上传图片尺寸为 690px  250px )</span>
        </div>
        <div class="layui-card-body margin-top-20 padding-bottom-0">
            <div class="padding-left-20" data-rule-list>
                <div class="layui-form-item text-center">
                    <a data-item-add >
<!--                        添加图片  -->
                    </a>
                </div>
            </div>
            <div class="hr-line-dashed margin-top-30"></div>
            <div class="layui-form-item text-center padding-left-20">
                <button class="layui-btn" data-submit>保存数据</button>
            </div>
        </div>
    </form>
</div>

<div data-item-tpl class="layui-hide">
    <div class="layui-form-item padding-left-0" data-rule-item>
        <div class="layui-input-inline nowrap relative margin-right-30" style="width:auto;">
            <input data-upload-image value='{$vo.img|default=""}' name="img[]" type="hidden">
        </div>
        <label class="layui-input-inline nowrap relative margin-bottom-5" style="width:300px">
            <b class="notselect color-green margin-right-5">图片名称</b>
            <input class="layui-input inline-block" style="width:240px" value='{$vo.menuname|default=""}' name="menuname[]" value="#" required placeholder="请输入图片名称">
<!--            <a data-item-up class="layui-btn layui-btn-primary"><i class="layui-icon layui-icon-up margin-0"></i></a>-->
<!--            <a data-item-dn class="layui-btn layui-btn-primary"><i class="layui-icon layui-icon-down margin-0"></i></a>-->
<!--            <a data-item-rm class="layui-btn layui-btn-primary"><i class="layui-icon layui-icon-close margin-0"></i></a>-->
        </label>
        <label class="layui-input-inline nowrap relative margin-bottom-5" style="width:300px">
            <b class="notselect color-green margin-right-5">跳转规则</b>
            <input class="layui-input inline-block" style="width:240px" value='{$vo.imgurl|default=""}' name="imgurl[]" value="#" placeholder="请输入跳转规则">
            <a data-prefix="NEWS" data-rule-page="{:url('data/news_item/select')}" class="layui-btn layui-btn-primary">选择文章</a>
            <a data-prefix="GOODS" data-rule-page="{:url('data/shop_goods/select')}" class="layui-btn layui-btn-primary">选择商品</a>
            <span class="help-block block notselect">若要跳转页面，请选择对应的数据或填写跳转的 URL 地址，不跳转请填写 “#” 号占位。</span>
        </label>
        <label class="layui-input-inline nowrap relative margin-bottom-5" style="width:300px">
            <b class="notselect color-green margin-right-5">跳转小程序appId</b>
            <input class="layui-input inline-block" style="width:240px" value='{$vo.mini_appid|default=""}' name="mini_appid[]" value="" placeholder="请输入跳转小程序appId">
            <span class="help-block block notselect">要跳转小程序，请填写小程序 appId。要跳转小程序首页，跳转规则留空。</span>
        </label>

    </div>
</div>

<label class="layui-hide">
    <textarea id="DefaultData">{$data|default=[]|json_encode}</textarea>
</label>

<style>
    [data-rule-page] {
        margin-top: -3px;
        margin-left: 5px;
    }

    [data-rule-item] {
        padding-left: 40px;
        margin-bottom: 20px;
    }

    [data-rule-item] .uploadimage {
        width: 135px;
        height: 100px;
    }

    [data-item-dn], [data-item-up], [data-item-rm] {
        margin-top: -4px;
        margin-left: 5px;
    }
</style>

<script>
    (function (data) {
        /*! 默认数据渲染 */
        if (data.length < 1) addItem();
        else data.forEach(function (item) {
            addItem(item)
        });
        /*! 初始化上传插件 */
        (function initUpload() {
            $('[data-rule-list] input[data-upload-image]').map(function () {
                if (!$(this).attr('inited')) $(this).attr('inited', true).uploadOneImage();
            });
            setTimeout(initUpload, 100);
        })();
        /*! 数据选项操作 */
        $('[data-rule-list]').on('click', '[data-rule-page]', function ($that) {
            $that = $(this), top.setItemValue = function (value) {
                $that.prevAll('input').val(($that.data('prefix') + '#{v}').replace('{v}', value));
            };
            $.form.iframe($(this).data('rule-page'), $that.data('title') || $that.text(), ['930px', '600px']);
        }).on('click', '[data-item-add]', function () {
            addItem();
        }).on('click', '[data-item-rm]', function () {
            $(this).parents('[data-rule-item]').remove();
        }).on('click', '[data-item-up]', function () {
            var item = $(this).parents('[data-rule-item]');
            var prev = item.prev('[data-rule-item]');
            if (item.index() > 0) item.insertBefore(prev);
        }).on('click', '[data-item-dn]', function () {
            var item = $(this).parents('[data-rule-item]');
            var next = item.next('[data-rule-item]');
            if (next) item.insertAfter(next);
        });
        /*! 表单提交处理 */
        $('form#DataForm').vali(function (ret) {
            var idx, data = [];
            for (idx in ret.img) {
                if (!ret.img[idx]) return $.msg.tips('请上传展示图片哦！');
                var mycars = new Array()
                data.push({img: ret.img[idx], imgurl: ret.imgurl[idx], menuname: ret.menuname[idx], mini_appid: ret.mini_appid[idx]});
            }
            $.form.load('{$request->url()}', {data: (data)}, 'post');
        });
    })(JSON.parse($('#DefaultData').val() || '[]') || []);

    /*! 添加数据选项 */
    function addItem(data) {
        this.$html = $($('[data-item-tpl]').html());
        if (data) for (this.index in data) this.$html.find('[name^="' + this.index + '"]').val(data[this.index]);
        $('[data-item-add]').parent().before(this.$html), setTimeout(function () {
            $.form.reInit();
        }, 100);
    }
</script>
{/block}
